package com.mywang.weitravel.modules.addfind;

import com.google.gson.Gson;
import com.mywang.weitravel.base.BasePresenter;
import com.mywang.weitravel.model.base.UploadResponse;
import com.mywang.weitravel.net.RetrofitHelper;
import com.mywang.weitravel.net.RxSchedulers;
import com.mywang.weitravel.net.VObserver;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.disposables.Disposable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by wangmingyun on 2019/12/10.
 */
public class AddFindPresenter extends BasePresenter<AddFindContract.View> implements AddFindContract.Presenter {
    public AddFindPresenter(AddFindContract.View view) {
        super(view);
    }

    @Override
    public void uploadPhotos(ArrayList<String> photoList) {
        List<MultipartBody.Part> parts = new ArrayList<>();
        for (int index = 0; index < photoList.size(); index++) {
            String photoPath = photoList.get(index);
            File photoFile = new File(photoPath);
            RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), photoFile);
            MultipartBody.Part part = MultipartBody.Part.createFormData("file" + index, photoFile.getName(), requestBody);
            parts.add(part);
        }
        RetrofitHelper.getInstance().getServer()
                .uploadFile(parts)
                .compose(RxSchedulers.applySchedulers())
                .as(bindToLife())
                .subscribe(new VObserver<UploadResponse>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
                        mView.showLoading();
                    }

                    @Override
                    protected void onSuccess(UploadResponse uploadResponse) {
                        mView.uploadPhotoSuccessful(uploadResponse.src);
                    }

                    @Override
                    protected void onFailure(int code, String message, boolean isShowMsg) {
                        super.onFailure(code, message, isShowMsg);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void publishDiscovery(String title, String content, List<String> thumb) {
        HashMap<String, String> params = new HashMap<>();
        params.put("title", title);
        params.put("content", content);
        params.put("thumb", new Gson().toJson(thumb));
        RetrofitHelper.getInstance().getServer()
                .publishDiscovery(params)
                .compose(RxSchedulers.applySchedulers())
                .as(bindToLife())
                .subscribe(new VObserver<Object>() {
                    @Override
                    protected void onSuccess(Object object) {
                        mView.publishSuccessful();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        mView.hideLoading();
                    }
                });
    }
}
